package disjunction.core.state
{
	import flash.events.EventDispatcher;
	
	/**
	 * A FSM state which can, as it is activated, receive data from the initiating state.
	 * @author Nick Wiggill
	 */
	
	public class State extends EventDispatcher
	{
		/**
		 * State's id, as used in the state manager dictionary. This is usually
		 * defined in a derived implementation class, but it can be set externally.
		 */
		private var _id:String;

		public function State(id:String)
		{
			_id = id;
		}
		
		/**
		 * (abstract?) State gains focus and begins main execution.
		 * @param data Any data which needs to be passed to the new gamestate.
		 */
		public function activate(data:Object = null):void
		{
			if (data)
			{
				initialise(data);
			}
		}

		/** 
		 * (abstract) Prepare the state if it is being activated for the first time.
		 * @param data The incoming data from a state change, if any. This enables
		 * the state to be initialised using data from the last state.
		 */
		
		public function initialise(data:Object):void
		{
			//trace("init state "+_id);
		}
		
		/**
		 * Called when the state must be removed from the stack.
		 */
		//TODO http://en.wikipedia.org/wiki/Dispose_pattern
		public function deactivate():void
		{
			//trace("deactivate state "+_id);
		}
		
		protected function change(toStateId:String, data:Object = null):void
		{
			dispatchEvent(new StateChangeEvent(StateChangeEvent.CHANGE, _id, toStateId, data));	
		}
		
		public function get id():String
		{
			return _id;
		}
	}
}